import {
	IndexApi
} from "@/api/IndexApi";

var jweixin = require('jweixin-module');

export default {
	//调试模式
	debug: false,
	//权限
	jsApiList: [
		'updateAppMessageShareData',
		'updateTimelineShareData',
		'getLocation'
	],
	//判断是否在微信中
	isWechat: function() {
		var ua = window.navigator.userAgent.toLowerCase();
		if (ua.match(/micromessenger/i) == 'micromessenger') {
			return true;
		} else {
			return false;
		}
	},

	//初始化sdk配置
	initJssdk: function(callback) {
		if (!this.isWechat()) {
			return;
		}
		//服务端进行签名 ，可使用uni.request替换。 签名算法请看文档

		IndexApi.getJsConfig(location.href.split('#')[0]).then((config) => {
			config['debug'] = this.debug
			config['jsApiList'] = this.jsApiList
			jweixin.config(config);
			//配置完成后，再执行分享等功能
			if (typeof callback === 'function') {
				callback();
			}
		})
	},

	//准备就绪
	ready: function(callback) {
		if (!this.isWechat()) {
			return;
		}
		this.initJssdk(function() {
			jweixin.ready(function() {
				//配置完成后，再执行
				if (typeof callback === 'function') {
					callback(jweixin);
				}
			})
		})
	},

	closeWindow: function() {
		this.ready(function(wx) {
			wx.closeWindow();
		})
	},

	mpShare(shareObject) {
		if (this.isWechat()) {
			this.ready(function(wx) {
				let data1 = {
					title: shareObject.wxPersonalDesc,
					imgUrl: shareObject.imgUrl,
					link: shareObject.link,
					success: function(e) {
					}
				}
				wx.updateTimelineShareData(data1, wx)   //新方法，此方法不回调

				let data2 = {
					title: shareObject.wxPersonalTitle,
					desc: shareObject.wxPersonalDesc,
					imgUrl: shareObject.imgUrl,
					link: shareObject.href
				}
				wx.updateAppMessageShareData(data2, wx)
			})
		}

	}
}
